#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/int32.hpp"  // 用于发布整数类型消息
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>

class RefereeNode : public rclcpp::Node {
public:
    RefereeNode() : Node("referee_node"), count_(0) {
        timer_ = this->create_wall_timer(
            std::chrono::seconds(1),
            std::bind(&RefereeNode::timer_callback, this));
        publisher_ = this->create_publisher<std_msgs::msg::Int32>("_time", 10);
    }

private:
    void timer_callback() {
        auto now = std::chrono::system_clock::now();
        std::time_t now_time = std::chrono::system_clock::to_time_t(now);
        std::tm* now_tm = std::localtime(&now_time);

        // 格式化系统时间
        std::ostringstream oss;
        oss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S");
        std::string formatted_time = oss.str();

        // 提取秒位
        int seconds = now_tm->tm_sec;

        // 打印系统时间和秒位
        RCLCPP_INFO(this->get_logger(), "系统时间: %s, 秒位: %d", formatted_time.c_str(), seconds);

        // 发布秒位数据
        std_msgs::msg::Int32 message;
        message.data = seconds;
        publisher_->publish(message);
    }
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr publisher_;
    size_t count_;
};

int main(int argc, char *argv[]) {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<RefereeNode>());
    rclcpp::shutdown();
    return 0;
}
